package org.linlinjava.litemall.wx.web;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.*;
import org.linlinjava.litemall.db.service.LitemallCategoryService;
import org.linlinjava.litemall.db.service.LitemallCommodityService;
import org.linlinjava.litemall.db.service.LitemallProvinceService;
import org.linlinjava.litemall.db.service.LitemallSeekHelpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 求助内容版块
 */
@Transactional
@RestController
@RequestMapping("/wx/seekHelp")
public class WxSeekHelpController {
    private final Log logger = LogFactory.getLog(WxSeekHelpController.class);
    @Autowired
    private LitemallSeekHelpService seekHelpService;
    //所有城市..所有L2服务
    @Autowired
    private LitemallCategoryService categoryService;
    @Autowired
    private LitemallProvinceService provinceService;
    @Autowired
    private LitemallCommodityService commodityService;

    //显示符合条件的求助..不传显示所有的求助内容.
    @PostMapping("/list")
    public Object seekHelp(@RequestBody String data){
        System.out.println("data>>>" + data);
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer page = jsonObject.getInteger("page");
        if (null == page || "".equals(page)){ page=1;}
        Integer limit = jsonObject.getInteger("limit");
        if (null == limit || "".equals(limit)){ limit = 10;}
        String sort= jsonObject.getString("sort");
        if(StringUtils.isEmpty(sort)){sort="add_time";}
        String order = jsonObject.getString("desc");
        if (StringUtils.isEmpty(order)){order="desc";}
        String province = jsonObject.getString("province");
        String area = jsonObject.getString("area");
        Map<String,String> searchMap=new HashMap<>();
        String title = jsonObject.getString("title");
        searchMap.put("area",area);
        searchMap.put("province",province);
        searchMap.put("title",title);

        PageInfo pageInfo = seekHelpService.seekHelpList(searchMap, page, limit, sort, order);
        System.out.println("seekHelpList?>>>" + pageInfo);
        return ResponseUtil.ok(pageInfo);
    }

    /**
     * 发布一条.求助信息
     * @param data
     * @return
     */
    @PostMapping("/addHelp")
    public Object addHelp(@RequestBody  String data){
        logger.info("发布求助>>"+ data);
        JSONObject jsonObject = JSONObject.parseObject(data);
        String title = jsonObject.getString("title");
        String province = jsonObject.getString("province");
        String area = jsonObject.getString("area");
        String telephone = jsonObject.getString("telephone");
        String sample = jsonObject.getString("sample");//固液气 ,和固液,液气,固气 混合体
        if (StringUtils.isEmpty(title) || title.length()<6){return ResponseUtil.fail(401,"标题title大于6个字");}
        if (StringUtils.isEmpty(province)){return ResponseUtil.fail(401,"请选择您的省份province");}
        if (StringUtils.isEmpty(area)){return ResponseUtil.fail(401,"请选择您的服务范围area");}
        if (StringUtils.isEmpty(telephone)){return ResponseUtil.fail(401,"请填写您的联系方式telephone");}
        LitemallSeekHelp litemallSeekHelp = new LitemallSeekHelp();
        litemallSeekHelp.setTitle(title);
        litemallSeekHelp.setProvince(province);
        litemallSeekHelp.setArea(area);
        litemallSeekHelp.setTelephone(telephone);
        litemallSeekHelp.setAddTime(LocalDateTime.now());
        litemallSeekHelp.setSample(sample);
        seekHelpService.addSeekHelp(litemallSeekHelp);
        return ResponseUtil.ok();
    }
    /**
     * 更改一条.求助信息
     * @param data
     * @return
     */
    @PutMapping("/updateHelp")
    public Object updateHelp(@RequestBody  String data){
        LitemallSeekHelp  litemallSeekHelp= JSONObject.parseObject(data, LitemallSeekHelp.class);
        //设置当时的时间
        litemallSeekHelp.setAddTime(LocalDateTime.now());
        seekHelpService.updateSeekHelp(litemallSeekHelp);
        return ResponseUtil.ok();
    }

    /*服务领域*/
    @GetMapping("/area")
    public Object list(){
        List<LitemallCategory> litemallCategoriesL2 = categoryService.queryL2();
        for (LitemallCategory categoryL2 : litemallCategoriesL2) {
            List<LitemallCategory> litemallCategoriesL3 = categoryService.queryByPid(categoryL2.getId());
            categoryL2.setChildCategory(litemallCategoriesL3);
            System.out.println("categoryL2>>>>>" + categoryL2);
        }
        return ResponseUtil.okList(litemallCategoriesL2);
    }

    //特色检测推介   //之前是2级分类..现在为3级.. 仍然是根据L2级分类查询
    @PostMapping("/recommend")
    public Object recommend(@RequestBody  String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        String nameL2 = jsonObject.getString("area");
        System.out.println("nameL2>>>" + nameL2);
        Integer page=1;
        Integer limit=6;
        String name="";
        String sort="";
        if(StringUtils.isNotEmpty(nameL2)){
            ResponseUtil.fail(402,"请增加服务范围area");
            LitemallCategory litemallCategory = categoryService.recommendBynameL2(nameL2);

            //根据L2级别的ID.判定..商品commodity..找几个返回
            Integer idL2 = litemallCategory.getId();
            System.out.println("idL2>>>" + idL2);
            PageInfo<LitemallCommodity> byL1L2L3 = commodityService.findByL1L2L3(page, limit, null,null, idL2, null, name ,sort);
            return ResponseUtil.ok(byL1L2L3);
        }
       // String brandId = jsonObject.getString("brandId");
        //如果没有关键字.随意返回几个
        PageInfo<LitemallCommodity> byL1L2L3 = commodityService.findByL1L2L3(page, limit, null,null, null, null, name,sort);
        return ResponseUtil.ok(byL1L2L3);

    }

    /**
     * 显示所有的省份    原来有方法region  废弃
     * @return
     */
    @GetMapping("allProvince")
    public Object allProvince(){
        List<LitemallProvince> litemallProvinces = provinceService.allProvinces();
        List<String> provinceList=new ArrayList<>();
        for (LitemallProvince litemallProvince : litemallProvinces) {
            provinceList.add(litemallProvince.getProvince());
        }
        return ResponseUtil.okList(provinceList);
    }

    /**
     * 删除求助信息
     * @param id
     * @return
     */
    @DeleteMapping("del/{id}")
    public Object delSeekHelp(@PathVariable("id") Integer id){

        seekHelpService.delSeekHelp(id);
        return  ResponseUtil.ok();
    }

    /**
     * 显示自己的求助信息
     * @return
     */
    @GetMapping
    public Object delSeekHelp(HttpServletRequest request,
                              @RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "10") Integer limit,
                              @Sort @RequestParam(defaultValue = "add_time") String sort,
                              @Order @RequestParam(defaultValue = "desc") String order
                              ){
        XxlSsoUser user = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        String userid = user.getUserid();
        Map<String,String> map=new HashMap<>();
        map.put("userId",userid);
        PageInfo pageInfo = seekHelpService.seekHelpList(map, page, limit, sort, order);
        return  ResponseUtil.ok(pageInfo);
    }
}
